package com.juntu.javaEE.pack;

import java.io.File;
import java.sql.*;
import java.util.*;

public class FinallyFileEXC {
	private static final String DRIVER = "com.mysql.jdbc.Driver";
	private static final String URL = "jdbc:mysql://101.37.255.152:3306/jdbc_resume";
	private static final String USER = "root";
	private static final String PASSWORD = "root";
	static int count;
	static Statement stmt = null;
	static int num;

	private static Set<String> fileNames = new HashSet<>();
	private static boolean flag = false;
	private static List<File> files = new ArrayList<>();

	public static void main(String[] args) {
		Connection con = null;

		ResultSet res = null;

		try {
			Class.forName(DRIVER);
			con = DriverManager.getConnection(URL, USER, PASSWORD);
			stmt = con.createStatement();
			String selectSql = "select name from resumename";
			res = stmt.executeQuery(selectSql);
			while (res.next()) {
				fileNames.add(res.getString("name"));
			}
			fileNames.add("");
			fileScreen();

			res.close();
			stmt.close();
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.out.println("简历筛选完成");
		System.out.println(count);
	}

	public static void fileScreen() throws SQLException {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入简历所在路径(输入0则使用默认路径C:/jianli)");
		// String path = "D:/jianli";
		String scStr = sc.next();
		String path = "0".equals(scStr) ? "C:/jianli" : scStr;
		File file = new File(path);
		if (!file.exists()) {
			System.out.println("没找到该文件夹");
			return;
		}

		getAllFile(file);
		for (File fs : files) {
			if (fs.isDirectory()) {
				System.out.println(fs.getName() + "目录");
			} else {
				String fileName = fs.getName();
				System.out.println(fileName);
				String newFileName = getNewFileName(fileName);
				System.out.println(newFileName);
				for (String fName : fileNames) {
					if (newFileName.equals(fName)) {
						flag = true;
						fs.delete();
						System.out.println("删除" + fs.getName());
						break;
					}
				}
				if (!flag) {
					fileNames.add(newFileName);
					String insertSql = "insert into resumename(name) value" + "('" + newFileName + "')";
					stmt.execute(insertSql);
					count++;
				}
				flag = false;
			}
		}
	}

	// 输入一个文件名，返回一个格式化之后的文件名
	public static String getNewFileName(String fileName) {
		String newFileName = null;
		String[] temp = fileName.split("_");
		if (temp.length <= 1) {
			newFileName = temp[0];
		} else {
			newFileName = temp[1] == null ? fileName : temp[1];
		}

		if (newFileName.length() > 4 && newFileName.indexOf("(") >= 0) {
			newFileName = newFileName.substring(0, newFileName.indexOf("("));
		}

		if (newFileName.length() > 4 && newFileName.indexOf("-") >= 0) {
			newFileName = newFileName.substring(0, newFileName.indexOf("-"));
		}

		return newFileName;
	}
	
	//遍历文件夹下所有的文件（包含子文件夹下的文件）
	public static void getAllFile(File f) {
		File[] fa;
		fa = f.listFiles();
		for (File file : fa) {
			if (file.isDirectory()) {
				getAllFile(file);
			} else {
				files.add(file);
			}
		}
	}
}
